package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DtsAdminServiceImpl implements DtsAdminService {

    @Autowired
    DtsAdminMapper adminMapper;

    @Override
    public DtsAdmin findByUsername(String username) {
        //根据用户名查询用户对象
        LambdaQueryWrapper<DtsAdmin> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.eq(DtsAdmin::getUsername, username);

        DtsAdmin admin = adminMapper.selectOne(queryWrapper);

        return admin;
    }

    @Override
    public IPage<DtsAdmin> queryPage(Integer page, Integer limit, String sort, String order, String username) {

        IPage<DtsAdmin> iPage = new Page(page,limit);
        QueryWrapper<DtsAdmin> queryWrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(username)){
            queryWrapper.like("username",username);
        }
        if(!StringUtils.isEmpty(order) && !StringUtils.isEmpty(sort)){
            if(order.equalsIgnoreCase("desc")){
                queryWrapper.orderByDesc(sort);
            }else {
                queryWrapper.orderByAsc(sort);
            }
        }

        IPage<DtsAdmin> iPage1 = adminMapper.selectPage(iPage, queryWrapper);
        return iPage1;
    }

    @Override
    public DtsAdmin insert(DtsAdmin admin) {
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setDeleted(false);

        BCryptPasswordEncoder bc = new BCryptPasswordEncoder();
        String password = bc.encode(admin.getPassword());
        admin.setPassword(password);
        adminMapper.insert(admin);
        return admin;
    }

    @Override
    public DtsAdmin update(DtsAdmin admin) {
        admin.setUpdateTime(new Date());
        BCryptPasswordEncoder bc = new BCryptPasswordEncoder();
        DtsAdmin admin1 = adminMapper.selectById(admin.getId());
        if(!bc.matches(admin.getPassword(),admin1.getPassword())){
            admin.setPassword(bc.encode(admin.getPassword()));
        }
        adminMapper.updateById(admin);
        return admin;
    }

    @Override
    public void delete(DtsAdmin admin) {
        admin.setUpdateTime(new Date());
        admin.setDeleted(true);
        adminMapper.updateById(admin);
    }

    @Override
    public List<Map<String, Object>> queryAdminList() {
        LambdaQueryWrapper<DtsAdmin> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsAdmin::getDeleted,false);
        List<DtsAdmin> dtsAdmins = adminMapper.selectList(qw);
        List<Map<String, Object>> maps = dtsAdmins.stream().map(admins -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", admins.getId());
            map.put("label", admins.getUsername());
            return map;
        }).collect(Collectors.toList());
        return maps;
    }
}
